﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Common;

public partial class website_admin_knowlegebase_kbcategoryinfoeditor : PageBase
{
    string CategoryImage = ConfigurationManager.AppSettings["CategoryImage"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin("system configuration");


        /// 保存设置
        if (RequestBase.GetFormString("__EVENTTARGET") == "save")
        {
            Save();
        }

        if (!IsPostBack)
        {
            CategoryDataBind();

            if (RequestBase.GetQueryString("otype") != "add")
                Initialize();
            else
            {
                if (RequestBase.GetQueryString("parentid") != "")
                {
                    Model.KbCategoryBase _obj = BLL.KbCategoryBase.GetObjById(RequestBase.GetQueryString("parentid"));

                    if (_obj != null)
                    {
                        selParentList.Value = _obj.CategoryId;
                        selTypeList.Value = _obj.CategoryType.ToString();
                        selTypeList.Disabled = true;
                    }
                }
            }
        }
    }

    #region 类别列表数据绑定
    void CategoryDataBind()
    {
        DataTable dt = BLL.CommonBLL.Select("KbCategoryBase", "*", " AND DeletionStateCode = 0 ORDER BY LineNum ASC");
        DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dt);
    }
    /// <summary>
    /// 类别列表邦定
    /// </summary>
    /// <param name="intLayer">级别</param>
    /// <param name="parentId">父目录ID</param>
    /// <param name="dt">目录数据表</param>
    void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
    {
        if (dt == null)
            return;

        DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

        ArrayList al = new ArrayList();
        ListItem li;

        string str = ""; int intCategoryLayerID = 0;
        foreach (DataRow d in dr)
        {
            str = "　| ";
            intCategoryLayerID = int.Parse(d["Layer"].ToString());
            if (intLayer == 1)
                str = " + ";
            for (int i = 1; i < intLayer * intLayer; i++)
                str += "--";

            li = new ListItem("\t" + str + "\t" + d["CategoryName"].ToString(), d["CategoryId"].ToString());
            selParentList.Items.Add(li);
            DropdownlistDataBind(intCategoryLayerID + 1, d["CategoryId"].ToString().Trim(), dt);
        }
    }
    #endregion

    #region 类别数据初始化
    void Initialize()
    {
        Model.KbCategoryBase _obj = BLL.KbCategoryBase.GetObjById(RequestBase.GetQueryString("categoryid"));
        CategoryObj = _obj;

        if (_obj.IsProtected)
        {
            selParentList.Disabled = true;
            cbxIsProtected.Disabled = true;
        }
    }

    public Model.KbCategoryBase CategoryObj
    {
        get
        {
            Model.KbCategoryBase _category = new Model.KbCategoryBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("categoryid")))
                _category.CategoryId = RequestBase.GetQueryString("categoryid");

            _category.CategoryName = txtCategoryName.Value.Trim();
            _category.Description = txtDescription.Value.Trim();
            _category.ParentId = selParentList.Value.Trim(); 
            _category.LineNum = int.Parse(txtLineNum.Value.Trim()); 
            _category.ModifiedOn = DateTime.Now;
            _category.CreatedOn = DateTime.Now;
            _category.ModifiedBy = this.SystemUserId;
            _category.CreatedBy = this.SystemUserId;
            _category.CategoryType = int.Parse(selTypeList.Value);
            _category.Tags = "";
            _category.IsProtected = cbxIsProtected.Checked;
            _category.ObjectLoweredName = txtObjectLoweredName.Value.Trim();
            _category.Pictures = txtUploadControl_CategoryImage.Value.Trim().Replace("|", ""); 

            return _category;
        }
        set
        {
            if (value != null)
            {
                txtCategoryName.Value = value.CategoryName.Trim();
                txtDescription.Value = value.Description.Trim(); 
                selParentList.Value = value.ParentId.Trim();
                txtOriginalParentId.Value = value.ParentId;
                txtLineNum.Value = value.LineNum.ToString(); 
                selTypeList.Value = value.CategoryType.ToString();
                cbxIsProtected.Checked = value.IsProtected;
                txtObjectLoweredName.Value = value.ObjectLoweredName;
                txtCategoryNumber.Value = "&amp;" + value.CategoryNumber;
                if (value.Pictures != "")
                {
                    txtUploadControl_CategoryImage.Value = "|" + value.Pictures.Trim();
                    divProductImagesList_CategoryImage.InnerHtml = "<div class=\"attachmentitems\">" + BLL.AttachmentsBase.GetIcoByExtName(value.Pictures.Trim().Split('*')[2]) + "&nbsp;&nbsp;<a href='../../" + CategoryImage + value.Pictures.Trim().Split('*')[1] + "' title='Click to open the file' target='_blank'>" + value.Pictures.Trim().Split('*')[0] + "</a>&nbsp;&nbsp; <a href='javascript:void(0);' filedata='" + value.Pictures.Trim() + "' itemid='CategoryImage' onclick='uploadinit($(this));'>[Delete]</a></div>";
                }
            }
        }
    }
    #endregion

    #region 保存数据
    void Save()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {
            res = BLL.KbCategoryBase.CreateNew(CategoryObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            res = BLL.KbCategoryBase.Modify(CategoryObj, (selParentList.Value.Trim() == txtOriginalParentId.Value.Trim()) ? false : true);
        }

        switch (res.Split('|')[0])
        {
            case "succeeded":
                JScript.AlertAndReturnValueToParent("信息保存成功!", "window.opener.document.location.reload(); window.close();");
                break; 
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}
